package com.mtf.mq.consumer;

import com.alibaba.fastjson.JSON;
import com.mtf.entity.MtfOrder;
import com.mtf.service.IMtfOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.mtf.common.enums.IdempotentEnum.MESSAGE_STATUS_IDEMPOTENT;
import static com.mtf.common.enums.IdempotentEnum.MESSAGE_TABLE_IDEMPOTENT;

/**
 * @Author matengfei
 * @Date 2024/3/18 11:01
 * @PackageName:com.mtf.mq.consumer
 * @ClassName: OrderRocketMqConsumer
 * @Description: 创建订单消费者
 * @Version 1.0
 */
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "springboot_consumer_group",topic = "idempotent_topic",consumeThreadMax = 1)
public class OrderRocketMqConsumer implements RocketMQListener<Message> {

    @Autowired
    private IMtfOrderService orderService;

    /**
     * @Description: 创建订单
     * @param message
     * @return void
     */
    @Override
    public void onMessage(Message message) {
        MtfOrder order = JSON.parseObject(message.getBody(), MtfOrder.class);
        log.info("订单开始创建，{}",order);
        //orderService.saveOrder(order,MESSAGE_TABLE_IDEMPOTENT);
        orderService.saveOrder(order,MESSAGE_STATUS_IDEMPOTENT);
    }
}
